package com.zhugang.march;

/**
 * @program algorithms
 * @description: shunzi
 * @author: chanzhugang
 * @create: 2022/03/12 23:50
 */
public class Shunzi {

    /**
     * 扑克牌中的顺子：
     * 随机抽5张牌，判断是不是顺子，即这5张牌是连续的，A为1，J为11，Q为12 K为13，大小王为0（可以看成任意数字）
     */
    public boolean isStraight(int[] nums) {
        /**
         * 满足：找规律分析
         * 1、不能重复
         * 2、去零
         * 3、max - min < 5 
         */
        // 存储扑克牌是否重复
        boolean[] dup = new boolean[14];
        int max = -1;
        int min = 100;
        for (int i = 0; i < 5; i++) {
            if (nums[i] != 0) {
                if (dup[nums[i]]) {
                    // 数组里出现过，重复了，
                    return false;
                } else {
                    // 没出现放在数组里
                    dup[nums[i]] = true;
                }
                // 遍历计算最大值和最小值
                if (nums[i] < min) {
                    min = nums[i];
                }
                if (nums[i] > max) {
                    max = nums[i];
                }
            }
        }
        return (max - min) < 5;
    }

    public static void main(String[] args) {
        Shunzi shunzi = new Shunzi();
        int[] nums = new int[]{1, 2, 0, 4, 5};
        boolean flag = shunzi.isStraight(nums);
        boolean stra = shunzi.isStra(nums);
        System.out.println(stra);
        System.out.println(flag);
    }


    public boolean isStra(int[] nums) {
        // 不重复、去零、max-min < 5
        int max = -1;
        int min = 100;
        boolean[] dump = new boolean[14];
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                if (dump[nums[i]]) return false;
                else dump[nums[i]] = true;
                if (nums[i] < min) min = nums[i];
                if (nums[i] > max) max = nums[i];
            }
        }
        return max - min < 5;
    }
}